package com.flick.student.ctrl;

import com.flick.po.Student;
import com.flick.student.resource.StudentResource;
import com.flick.student.resource.StudentResourceAssembler;
import com.flick.student.service.StudentServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.*;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

/**
 * @author flick on 2017/5/4.
 * @version 1.0
 */
@RestController
@ExposesResourceFor(Student.class)
@RequestMapping(value = "/students")
public class StudentCtrl {
    @Autowired
    StudentServ student;
//    @Autowired
//    private EntityLinks entityLinks;

    @RequestMapping(method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public Resources<StudentResource> list(){
//        return student.list();
        Link link = linkTo(StudentCtrl.class).withSelfRel();
        return new Resources<StudentResource>(new StudentResourceAssembler().toResources(student.list()), link);
//        Link link = linkTo(Student.class).withSelfRel();
//        return new Resources<Student>(new ListResourceAssembler().toResources(listService.findByUserUsername(username)), link);
    }
//
    @RequestMapping(value = "/{id}",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public Resource<Student> get(@PathVariable String id){
        return new StudentResourceAssembler().toResource(student.get(id));
    }
//
//
//    private Resource<Student> getAlbumResource(Student student) {
//
//        Resource<Student> resource = new Resource<Student>(student);
//
//        // Link to Album
//        resource.add(linkTo(methodOn(StudentCtrl.class).get(student.getId())).withSelfRel());
//        // Link to Artist
////        resource.add(linkTo(methodOn(StudentCtrl.class).getArtist(student.getArtist().getId())).withRel("artist"));
//        // Option to purchase Album
////        if (student.getStockLevel() > 0) {
////            resource.add(linkTo(methodOn(StudentCtrl.class).purchaseAlbum(student.getId())).withRel("album.purchase"));
////        }
//
//        return resource;
//
//    }
}
